package sistema.web.dao.impls;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import sistema.web.dao.IClubDAO;
import sistema.web.dtos.ClubDTO;
import sistema.web.utils.MySQLConnection;

public class ClubDAO implements IClubDAO {

	public List<ClubDTO> listar(String campo, Object valor) {
		List<ClubDTO> listClub = null;
		
		try {
			Connection conn = MySQLConnection.getConnection();
			
			String query = 
				"SELECT " + 
				"   * " +
				"FROM " + 
				"	club "
			;
			
			if(campo != null && valor != null) {
				if(!campo.equals("idClub")) {
					query += 
						" WHERE " +
							campo + " like '%" + valor + "%'"
					;					
				} else {
					query += 
						" WHERE " +
							campo + " = " + valor
					;					
				}			
			}
			
			PreparedStatement pst = conn.prepareStatement(query);
			ResultSet rs = pst.executeQuery();
			listClub = new ArrayList<ClubDTO>();
			while(rs.next()) {
				ClubDTO dtoClub = new ClubDTO();
				
				dtoClub.setIdClub(rs.getInt("idClub"));
				dtoClub.setNombre(rs.getString("Nombre"));
				dtoClub.setDireccion(rs.getString("Direccion"));
				dtoClub.setTelefono(rs.getString("Telefono"));
				
				listClub.add(dtoClub);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listClub;
	}
}